package com.yun.util.clickHouse;


import com.yun.util.clickHouse.bean.DbFiled;
import com.yun.util.clickHouse.bean.TableClassBean;

import java.util.List;

public class CreatTableSqlUtil {


    /**
     * CREATE TABLE [IF NOT EXISTS] [db.]table_name ON CLUSTER cluster
     * (
     *     name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
     *     name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
     *     ...
     *     INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1,
     *     INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2
     * ) ENGINE = engine_name()
     * [PARTITION BY expr]
     * [ORDER BY expr]
     * [PRIMARY KEY expr]
     * [SAMPLE BY expr]
     * [SETTINGS name=value, ...];
     * @param tableClassBean
     * @return
     */
    public static String getTableNameList(TableClassBean tableClassBean){
        StringBuilder sql = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
        sql.append(tableClassBean.getTbName()).append("\n").append("(");
        List<DbFiled> dbFiledList = tableClassBean.getDbFiledList();
        for (int i = 0;i<dbFiledList.size();i++){
            sql.append(dbFiledList.get(i).getColumName());
            if (dbFiledList.get(i).isNullable()){
                sql.append(" Nullable(").append(dbFiledList.get(i).getColumType()).append(")");
            }else {
                sql.append(" ").append(dbFiledList.get(i).getColumType());
            }
            if (i != dbFiledList.size()-1){
                sql.append(",");
            }
        }

        sql.append(") ").append("ENGINE = ").append(tableClassBean.getEngine()).append("\n");
        if (tableClassBean.getParititionBy() != null && !tableClassBean.getParititionBy().equals("") ){
            sql.append("PARTITION BY ").append(tableClassBean.getParititionBy()).append("\n");
        }
        if (tableClassBean.getOrderBy() != null && !tableClassBean.getOrderBy() .equals("")){
            sql.append("ORDER BY ").append(tableClassBean.getOrderBy()).append("\n");
        }
        if (tableClassBean.getPrimaryBy() != null && !tableClassBean.getPrimaryBy() .equals("")){
            sql.append("PRIMARY KEY ").append(tableClassBean.getPrimaryBy()).append("\n");
        }
        if (tableClassBean.getSample() != null && !tableClassBean.getSample() .equals("")){
            sql.append("SAMPLE BY ").append(tableClassBean.getSample()).append("\n");
        }
        if (tableClassBean.getSettings() != null && !tableClassBean.getSettings() .equals("")){
            sql.append("SETTINGS ").append(tableClassBean.getSettings()).append("\n");
        }
        if (tableClassBean.getTtlExprs().length>0){
            sql.append("TTL");
            for (int i = 0;i<tableClassBean.getTtlExprs().length;i++){
                sql.append(" ").append(tableClassBean.getTtlExprs()[i]);
                if (i != tableClassBean.getTtlExprs().length-1){
                    sql.append(",").append("\n");
                }
            }
        }
        sql.append(";");

        return sql.toString();
    }

    /**
     * ALTER TABEL db_name.table_name ADD COLUMN `$user_id` Nullable(String);
     */
    public static String getAlterTable(TableClassBean tableClassBean){
        StringBuilder sql = new StringBuilder("ALTER TABLE ");
        sql.append(tableClassBean.getTbName());
        List<DbFiled> dbFiledList = tableClassBean.getDbFiledList();
        for (int i = 0;i<dbFiledList.size();i++){
            sql.append(" ADD COLUMN ");
            sql.append(dbFiledList.get(i).getColumName());
            if (dbFiledList.get(i).isNullable()){
                sql.append(" Nullable(").append(dbFiledList.get(i).getColumType()).append(")");
            }else {
                sql.append(" ").append(dbFiledList.get(i).getColumType());
            }
            if (i == dbFiledList.size()-1){
                sql.append(";");
            }
        }
        return sql.toString();
    }

    /**
     * ALTER TABLE example_table
     *     MODIFY TTL d + INTERVAL 1 DAY;
     */
    public static String getAlterTableTTL(TableClassBean tableClassBean){
        StringBuilder sql = new StringBuilder("ALTER TABLE ");
        sql.append(tableClassBean.getTbName()).append(" MODIFY TTL ");
        for (int i = 0;i<tableClassBean.getTtlExprs().length;i++){
            sql.append(" ").append(tableClassBean.getTtlExprs()[i]);
            if (i != tableClassBean.getTtlExprs().length-1){
                sql.append(",").append("\n");
            }else {
                sql.append(";");
            }
        }
        return sql.toString();
    }
}
